/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div class="item-title">
      <router-link
        class="back"
        :to="{name:'stores',params:{currentPage:$route.params.currentPage}}"
        title="返回列表"
      >
        <i class="fa fa-arrow-circle-o-left"></i>
      </router-link>
      <div class="subject">
        <h3>店铺管理 - 编辑店铺 <{{this.$route.params.storeName}}> 的经营类目</h3>
      </div>
    </div>
    <div class="mDiv">
      <div class="ftitle">
        <h3>经营类目列表</h3>
        <h5>(共 {{tableData.length}} 条记录)</h5>
      </div>
      <a href @click.prevent="refresh()" class="refresh-date">
        <div title="刷新数据" class="pReload">
          <i class="fa fa-refresh"></i>
        </div>
      </a>
    </div>
    <div class="tablebox">
      <el-table :data="tableData" ref="multipleTable" style="width: 100%;">
        <el-table-column label="分类一" prop="class_one_name" width="150"></el-table-column>
        <el-table-column label="分类二" prop="class_two_name" width="150"></el-table-column>
        <el-table-column label="分类三" prop="class_three_name" width="150"></el-table-column>
        <el-table-column label="分佣比例" prop="commis_rate" width="150"></el-table-column>
        <el-table-column label="状态" prop="state_desc" width="150"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="remove(scope.row.bid)" class="el-icon-delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="item-title" style="margin-top:20px">
      <div class="subject">
        <h3>添加经营类目</h3>
      </div>
    </div>
    <el-cascader
      size="medium"
      v-model="category"
      :options="goodsCategoryTree"
      :props="{label: 'name', value: 'id',expandTrigger: 'hover'}"
    />
    <br />
    <el-button type="primary" size="medium" @click="add()">添 加</el-button>
  </div>
</template>

<script>
import {
  getStoreBindClasses,
  getGoodsCategoryTree,
  addStoreBindClass,
  deleteStoreBindClass
} from "@/utils/api";
export default {
  data() {
    return {
      tableData: [],
      store: {},
      currentPage: this.$route.params.currentPage || 1,
      storeId: this.$route.params.storeId,
      category: [],
      goodsCategoryTree: []
    };
  },
  components: {},
  created() {
    this.getList();
    this.getGoodsCategoryTree();
  },
  methods: {
    getList() {
      var that = this;
      getStoreBindClasses({
        store_id: this.storeId
      }).then(function(res) {
        that.tableData = res;
      });
    },
    getGoodsCategoryTree() {
      var that = this;
      getGoodsCategoryTree({
        levels: "1,2,3"
      }).then(function(res) {
        that.goodsCategoryTree = res;
      });
    },
    refresh() {
      this.getList();
      this.$message({
        type: "success",
        message: "刷新成功！"
      });
    },
    add() {
      if (this.category.length < 2) {
        this.$message.error("请选择经营类目!");
        return;
      }
      var storeBindClass = {
        store_id: this.storeId,
        class1: this.category[0],
        class2: this.category[1],
        class3: this.category[2],
        state: 1
      };
      var that = this;
      addStoreBindClass(storeBindClass).then(function(res) {
        if (res.status == 1) {
          that.getList();
          that.$message({
            type: "success",
            message: "添加成功!"
          });
        } else {
          that.$message.error("添加失败");
        }
      });
    },
    remove(bid) {
      var that = this;
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteStoreBindClass(bid).then(function(res) {
            if (res.status == 1) {
              that.getList();
              that.$message({
                type: "success",
                message: "删除成功!"
              });
            } else {
              that.$message.error({
                message: res.msg
              });
            }
          });
        })
        .catch(() => {
          that.$message.info({
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style scoped>
</style>
